package com.itheima.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.PGResult;
import com.itheima.common.PermissionResult;
import com.itheima.common.UserPage;
import com.itheima.mapper.PGMapper;
import com.itheima.mapper.PPGMapper;
import com.itheima.pojo.PermissionPermissionGroup;
import com.itheima.pojo.pe.PeDto;
import com.itheima.pojo.pe.pe_permission_group;
import com.itheima.service.PGService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PGServiceImpl implements PGService {
    @Autowired
    private PGMapper pgMapper;
    @Autowired
    private PPGMapper ppgMapper;
    @Override
    public UserPage list(Long page, Long pagesize, String title) {
        IPage<pe_permission_group> p = new Page<>(page, pagesize);
        LambdaQueryWrapper<pe_permission_group> wrapper = new LambdaQueryWrapper<>();
        if (title!=null){
            wrapper.like(pe_permission_group::getTitle,title);
        }
        IPage<pe_permission_group> pe_permission_groupIPage = pgMapper.selectPage(p, wrapper);
        List<pe_permission_group> list = pe_permission_groupIPage.getRecords();
        List<PGResult> pgResultList=new ArrayList<>();
        for (pe_permission_group pe_permission_group : list) {
            PGResult pgResult = new PGResult();
            pgResult.setCreate_date(pe_permission_group.getCreateTime());
            pgResult.setUpdate_date(pe_permission_group.getUpdateTime());
            pgResult.setId(pe_permission_group.getId());
            pgResult.setTitle(pe_permission_group.getTitle());
            pgResultList.add(pgResult);
        }
        UserPage userPage = new UserPage();
        userPage.setCounts(pe_permission_groupIPage.getTotal());
        userPage.setPage(page);
        userPage.setList(pgResultList);
        userPage.setPageSize(pagesize);
        userPage.setPages(pe_permission_groupIPage.getPages());
        return userPage;
    }

    @Override
    public PermissionResult modify(Integer id) {
        pe_permission_group pe_permission_group = pgMapper.selectById(id);
        PermissionResult permissionResult = new PermissionResult();
        LambdaQueryWrapper<PermissionPermissionGroup> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PermissionPermissionGroup::getPgid,id);
        List<PermissionPermissionGroup> ppglist = ppgMapper.selectList(wrapper);
        List<Long> permissions=new ArrayList<>();
        for (PermissionPermissionGroup ppg : ppglist) {
            permissions.add(ppg.getPid());
        }
        permissionResult.setCreate_date(pe_permission_group.getCreateTime());
        permissionResult.setId(id);
        permissionResult.setTitle(pe_permission_group.getTitle());
        permissionResult.setPermissions(permissions);
        return permissionResult;
    }

    @Override
    public void modifyPermission(Long id, PeDto peDto) {
        //先删除之前保存的权限ID
        LambdaQueryWrapper<PermissionPermissionGroup> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PermissionPermissionGroup::getPgid, id);
        ppgMapper.delete(wrapper);
        //重新添加新的
        List<Integer> permissionIds = peDto.getPermissions();
        for (Integer permissionId : permissionIds) {
            PermissionPermissionGroup ppg = new PermissionPermissionGroup();
            ppg.setPgid(id);
            ppg.setPid((long) permissionId);
            ppgMapper.insert(ppg);
        }
        //更改权限组内的数据
        pe_permission_group pg = pgMapper.selectById(id);
        pg.setTitle(peDto.getTitle());
        pgMapper.updateById(pg);
    }

    @Override
    public void removeById(Long id) {
        //先删除之前保存的权限ID
        LambdaQueryWrapper<PermissionPermissionGroup> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PermissionPermissionGroup::getPgid, id);
        ppgMapper.delete(wrapper);
        //再删除权限组
        pgMapper.deleteById(id);
    }

    @Override
    public List<PGResult> permissionSimple() {
        List<PGResult> resultList =new ArrayList<>();
        List<pe_permission_group> list = pgMapper.selectList(null);
        for (pe_permission_group pe_permission_group : list) {
            PGResult pgResult = new PGResult();
            pgResult.setId(pe_permission_group.getId());
            pgResult.setTitle(pe_permission_group.getTitle());
            resultList.add(pgResult);
        }
        return resultList;
    }
}
